package com.duyutian.util;

import com.duyutian.domain.Loger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisTools {
    private static final Logger log = LoggerFactory.getLogger(RedisTools.class);
    @Autowired
    private RedisTemplate redisTemplate;

    public void remove(String... keys) {
        String[] var2 = keys;
        int var3 = keys.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String key = var2[var4];
            this.remove(key);
        }

    }

    public void removePattern(String pattern) {
        Set keys = this.redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            this.redisTemplate.delete(keys);
        }

    }

    public void remove(String key) {
        if (this.exists(key)) {
            this.redisTemplate.delete(key);
        }

    }

    public void expire(String key, Long expireTime) {
        if (this.exists(key)) {
            this.redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        }
    }

    public boolean exists(String key) {
        return this.redisTemplate.hasKey(key).booleanValue();
    }


    public <T> T get(String key) {
        Object result = null;
        ValueOperations operations = this.redisTemplate.opsForValue();
        result = operations.get(key);
        return (T) result;
    }

    public <T> List<T> multiGet(List<String> keys) {
        ValueOperations operations = this.redisTemplate.opsForValue();
        return (List<T>) operations.multiGet(keys);
    }

    public boolean set(String key, Object value) {
        return set(key, value, null);
    }

    public boolean setOneHour(String key, Object value) {
        return set(key, value, 3600l);
    }

    public boolean set(String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations e = this.redisTemplate.opsForValue();
            if (expireTime == null) {
                e.set(key, value);
            } else {
                e.set(key, value, expireTime.longValue(), TimeUnit.SECONDS);
            }
            result = true;
        } catch (Exception var6) {
            log.error("RedisUtil set exception", var6);
        }

        return result;
    }

    public boolean setIfAbsent(String key, Object value) {
        boolean result = false;

        try {
            ValueOperations e = this.redisTemplate.opsForValue();
            result = e.setIfAbsent(key, value).booleanValue();
        } catch (Exception var5) {
            log.error("RedisUtil setIfAbsent exception", var5);
        }

        return result;
    }

    public boolean setIfAbsent(String key, Object value, Long expireTime) {
        boolean result = false;

        try {
            ValueOperations e = this.redisTemplate.opsForValue();
            result = e.setIfAbsent(key, value).booleanValue();
            if (result) {
                expire(key, expireTime.longValue());
            }
        } catch (Exception var6) {
            log.error("RedisUtil setIfAbsent exception", var6);
        }

        return result;
    }

    public boolean tryLock(String key, Long cacheSeconds) {
        boolean isLock = false;

        try {
            isLock = this.setIfAbsent(key, "", cacheSeconds);
        } catch (Exception var5) {
            log.error("RedisUtil tryLock exception", var5);
        }

        return isLock;
    }

    //集合类操作 list

    /**
     * 在名称为key的list开始位置加一个值为value的元素
     *
     * @param key
     * @param values
     * @return
     */


    public Long listLeftPush(String key, Object... values) {
        return listLpush(key, values, null);
    }

    public Long listLeftPush(String key, List<Object> values) {
        return listLpush(key, null, values);
    }

    private long listLpush(String key, Object[] var1, List<Object> var2) {
        Long count = 0l;
        try {
            ListOperations op = this.redisTemplate.opsForList();
            if (var1 != null) {
                count = op.leftPushAll(key, var1);
            }
            if (var2 != null) {
                count = op.leftPushAll(key, var2);
            }
            return count;
        } catch (Exception var6) {
            log.error("RedisUtil rpush exception", var6);
            return 0l;
        }
    }

    /**
     * 在名称为key的list末尾加一个值为value的元素
     *
     * @param key
     * @param values
     * @return
     */
    public Long listRightPush(String key, Object... values) {
        return listRpush(key, values, null);
    }

    public Long listRightPushAll(String key, List<Object> values) {
        return listRpush(key, null, values);
    }

    private long listRpush(String key, Object[] var1, List<Object> var2) {
        Long count = 0l;
        try {
            ListOperations op = this.redisTemplate.opsForList();
            if (var1 != null) {
                count = op.rightPushAll(key, var1);
            }
            if (var2 != null) {
                count = op.rightPushAll(key, var2);
            }
            return count;
        } catch (Exception var6) {
            log.error("RedisUtil rpush exception", var6);
            return 0l;
        }
    }

    /**
     * 取出list中存取的元素
     *
     * @param key
     * @param start 开始位置 0
     * @param end   结束位置 -1 最后一位
     * @param <T>
     * @return
     */
    public <T> List<T> listRange(String key, long start, long end) {
        try {
            ListOperations op = this.redisTemplate.opsForList();
            return (List<T>) op.range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("leftPop 操作失败 " + e.getMessage());
            return null;
        }
    }

    /**
     * lpop(key)：返回并删除名称为key的list中的首元素
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> T listLeftPop(String key) {
        try {
            ListOperations op = this.redisTemplate.opsForList();
            return (T) op.leftPop(key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("leftPop 操作失败 " + e.getMessage());
            return null;
        }
    }

    /**
     * rpop(key)：返回并删除名称为key的list中的尾元素
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> T listRightPop(String key) {
        try {
            ListOperations op = this.redisTemplate.opsForList();
            return (T) op.rightPop(key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("rightPop 操作失败 " + e.getMessage());
            return null;
        }
    }

    //集合类操作 list  end

    // hash map 数据类型 start ==
    public boolean mapPut(String key, String field, Object value) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            op.put(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean mapPutAll(String key, Map map) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            op.putAll(key, map);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public <T> T mapGetField(String key, Object field) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            return (T) op.get(key, field);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public <T> T mapGetFields(String key, List<Object> fields) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            return (T) op.multiGet(key, fields);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public <T> T mapGetFieldAll(String key) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            return (T) op.entries(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public long mapDelField(String key, String... fields) {
        try {
            HashOperations op = this.redisTemplate.opsForHash();
            return op.delete(key, fields);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    // hash map 数据类型 end ==


    // set 数据类型 start ====

    // set 数据类型 start ====


    //如果有该ip返回数组
    public ArrayList<Loger> getLogs() {
        return (ArrayList<Loger>) get("logs");
    }
    public void addLogs(Loger loger) {
        ArrayList<Loger> logs = (ArrayList<Loger>) get("logs");
        logs.add(loger);
        set("logs", logs);
    }
    public void serLogs(ArrayList<Loger> arr){
        set("logs",arr);
    }

    //判断是否有该访客
    public boolean hasIp(String ip) {
        ArrayList<Loger> logs = (ArrayList<Loger>) get("logs");
        for (Loger l:
        logs) {
            if(l.getIp().equals(ip)){
                return true;
            }
        }
        return false;
    }
}

